package utils
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setTimeout;

	public class Effect
	{
		/**
		 * 打字效果 
		 * @param textTF
		 * @param text
		 * 
		 */
		public static function textEffect(textTF:TextField,text:String):void{
			textTF.text = "";
			var t:Timer = new Timer(60);
			t.addEventListener(TimerEvent.TIMER,onTime);
			var i:int=0;
			function onTime(e:Event):void{
				if(i>text.length) {
				     t.removeEventListener(TimerEvent.TIMER,onTime);
					 t.stop();
				}
			    textTF.appendText(text.charAt(i++));
			}
		    t.start();
		}
		
		/**
		 * 数字滚动. 
		 * @param textTF
		 * @param text
		 * 
		 */
		public static function textRollEffect(textTF:*,text:int,speed:int=2):void{
			var ob:EffectText = new EffectText(textTF);
			TweenMax.to(ob,speed,{value:text});   
			
		}
		/**
		 * 让一个显示对象发光闪烁 
		 * @param d displayObject
		 * @param color 发光颜色
		 * @param count 闪烁次数
		 * @return object  isStop=true,立即停止闪烁
		 * 
		 */
		public static function flash(d:DisplayObject,color:uint=0x91e600,count:int=-1,blur:int = 20,strength:int = 3):Object{
			var b:Boolean=false;
			var o:Object={isStop:false};
			fun();
			function fun():void{
				if(o.isStop && !b){
					d.filters=null;
					return;
				}
				if(count==0 && !b){
					d.filters=null;
					return;
				}
				if(count>0){
					count--;
				}
				if(b){
					TweenMax.to(d, 0.5, {glowFilter:{color:color, alpha:0, blurX:0, blurY:0},onComplete:fun});
				}else{
					TweenMax.to(d, 0.5, {glowFilter:{color:color, alpha:1, blurX:blur, blurY:blur, strength:strength},onComplete:fun});	
				}
				b=!b;
			}
			return o;
		}
		
		/**
		 * 
		 * 躲闪幻影的效果
		 */
		public static function coverEffect(d:DisplayObject,fun:Function=null):void{
			var p:Point=new Point(d.x,d.y);	
			var timeline:TimelineLite = new TimelineLite();
			timeline.append( TweenLite.to(d, 0.2, {x:p.x+8, y:p.y+8, motionBlur:true}) );
			timeline.append( TweenLite.to(d, 0.2, {x:p.x-8, y:p.y-8, motionBlur:true}) );
			timeline.append( TweenLite.to(d, 0.2, {x:p.x, y:p.y, motionBlur:true,onComplete:fun}));
			//timeline.insertMultiple( TweenMax.allTo([box1, box2, box3], 1, {tint:0x90E500, scaleX:0.2, scaleY:0.2}), timeline.duration );
		}
		
		/**
		 * 
		 * 躲闪幻影的效果
		 */
		public static function hitEffect(d:DisplayObject,isE:Boolean,fun:Function=null):void{
			var p:Point=new Point(d.x,d.y);	
			var timeline:TimelineLite = new TimelineLite();
			if(isE){
				timeline.append( TweenLite.to(d, 0.2, {x:p.x+8, y:p.y-8, motionBlur:true}) );
			}else{
				timeline.append( TweenLite.to(d, 0.2, {x:p.x-8, y:p.y+8, motionBlur:true}) );
			}
			timeline.append( TweenLite.to(d, 0.2, {x:p.x, y:p.y, motionBlur:true,onComplete:fun}));
			//timeline.insertMultiple( TweenMax.allTo([box1, box2, box3], 1, {tint:0x90E500, scaleX:0.2, scaleY:0.2}), timeline.duration );
		}
		/**
		 * 设置对象震动。 
		 * @param d
		 * @param 震动时间
		 */
		public static function shake(d:DisplayObject,time:int=-1,callBack:Function=null):Shake{
			var shake:Shake = new Shake(10,10); ;  
			shake.addList(d.name, d);
			shake.start(d.name);
			if(time>0){
				setTimeout(back,time);
			}
			function back():void{
				shake.stop();
				if(callBack!=null){
					callBack();
				}
			}
			return shake;
		}
	}
}
import flash.text.TextField;
class EffectText{
     private var _value:int;
	 private var _text:*;
	 function EffectText(tf:*):void{
	      _text = tf;
		  _value=int(tf.text)
	 }
	 
	 public function get value():int
	 {
		 return _value;
	 }

	 public function set value(v:int):void
	 {
		 _value = int(v);
		 _text.text=String(v);
	 }
}